<?php


namespace app\seller\controller;

use think\Db;
use think\Config;


class Placeorder extends Base
{

    public function member()
    {
        $length = input('length');
        $keyword = input('keyword');
        $lists = Db::name("user")->field("id,username,mobile,avatar")->limit($length, 50)->where('username|mobile', "like", "%$keyword%")->select();
        return $this->success('ok', $lists);
    }

    public function detail()
    {
        $fangjian_id = input('fangjian_id');
        $data = Db::name("fangjian")->where('id', $fangjian_id)->find();
        if (empty($data)) {
            return $this->error('房间不存在');
        }
        $data['image'] =cdnurl($data['image'], true);

        $no_free_time = [];
        $times = strtotime(date('Y-m-d 00:00:00')) - 86400 * 2;
        $order_list = Db::name('order')->field('id,fuwu,fuwu_sc')->where([
            'fangjian_id' => $fangjian_id,
            'status' => ['in', ['1', '2', '5']]
        ])->where("UNIX_TIMESTAMP(fuwu) > " . $times)->select();
        $duration = $data['duration'];
        if (!empty($order_list)) {
            foreach ($order_list as $key => $order) {
                $start_time = $order['fuwu'];
                $end_time = date('Y/m/d H:i:s', strtotime($order['fuwu']) + $order['fuwu_sc'] * 3600);
                $no_free_time = array_merge($no_free_time, $this->time_data($start_time, $end_time, $duration));
            }
        }
        $look_time = Db::name('look_time')->where([
            'fangjian_id' => $fangjian_id,
        ])->where("UNIX_TIMESTAMP(start_time) > " . $times)->select();
        if ($look_time) {
            foreach ($look_time as $key => $time) {
                $start_time = $time['start_time'];
                $end_time = date('Y/m/d H:i:s', strtotime($time['start_time']) + $time['end_time'] * 60);
                $no_free_time = array_merge($no_free_time, $this->time_data($start_time, $end_time, $duration));
            }
        }
        $data['images'] = explode(',', $data['images']);
        foreach ($data['images'] as &$v) {
            $v = cdnurl($v, true);
        }
        $data['no_free_time'] = $no_free_time;
        $data['dizhi'] = Db::name("category")->where('id', $data['category_id'])->value('dizhi');
        $data['phone'] = Db::name("category")->where('id', $data['category_id'])->value('phone');
        $data['lng'] = Db::name("category")->where('id', $data['category_id'])->value('lng');
        $data['lat'] = Db::name("category")->where('id', $data['category_id'])->value('lat');
        $data['business_stare_time'] = Db::name("category")->where('id', $data['category_id'])->value('business_stare_time'); // 开始营业时间
        $data['business_end_time'] = Db::name("category")->where('id', $data['category_id'])->value('business_end_time'); // 开始营业时间
        $data['taocan'] = Config::get('site.taocan');
        $data['package'] = Db::name('fangjian_package')->where(['fj_id' => $fangjian_id])->select();
        $this->success('查询成功', $data);
    }


    public function time_data($start_time, $end_time, $duration)
    {
        $timeArr = [];
        $duration_time = $duration == 1 ? 30 : 15;
        for ($i = strtotime($start_time); $i <= strtotime($end_time); $i = $i + 60 * $duration_time) {
            $timeArr[] = date('Y/m/d H:i:s', $i);
        }
        return $timeArr;
    }

    public function buy()
    {
        $fangjian_id = input('fangjian_id');
        $fuwu = input()['selectedDate'][0]['date'];
        $fuwu_sc = input('fuwu_sc');
        $user_id = input('user_id');
        $package_id = input('package_id');
        $type = input('type');
        if (empty($fangjian_id)) {
            return $this->error('房间id不能为空');
        }
        if (empty($fuwu)) {
            return $this->error('服务时间不能为空');
        }
        if (empty($fuwu_sc)) {
            return $this->error('服务时长不能为空');
        }
        if (empty($user_id)) {
            return $this->error('请先登录');
        }
        if (empty($farr = Db::name("fangjian")->where('id', $fangjian_id)->find())) {
            return $this->error('房间不存在');
        }
        $k_time = strtotime($fuwu);

        $order = Db::name('order')->field('id,fuwu,fuwu_sc')->where([
            'fangjian_id' => $fangjian_id,
            'status' => ['in', ['1', '2', '5']]
        ])->where("UNIX_TIMESTAMP(fuwu) > " . time())->select();
        $astart = $k_time;//第1个开始
        $aend = $k_time + $fuwu_sc * 60 * 60;//第1个结束
        foreach ($order as &$v) {
            $q_time = strtotime($v['fuwu']);
            $f_time = $q_time + $v['fuwu_sc'] * 60 * 60;
            $bstart = $q_time;//第2个开始
            $bend = $f_time;//第2个结束
            $isIntersection = false;
            if ($bstart - $astart > 0) {
                if ($bstart - $aend <= 0) {
                    $isIntersection = true;
                }
            } else {
                if ($bend - $astart > 0) {
                    $isIntersection = true;
                }
            }
            if ($isIntersection == true) {
                return $this->error('房间该时间段已有人预订');
            }
        }
        $fangjian = Db::name("fangjian")->where('id', $fangjian_id)->find();

        $is_package = 0;
        if ($type == 0) {
            $price = $fuwu_sc * $fangjian['price'];
        } else {
            $price = Db::name('fangjian_package')->where(['fj_id' => $fangjian_id, 'id' => $package_id])->value('price');
            if (!$price) {
                return  $this->error('套餐不存在');
            }
            $is_package = 1;
        }
        $data = [
            'fangjian_id' => $fangjian_id,
            'createtime' => time(),
            'price' => $price,
            'org_price' => $price,
            'real_price' => $price,
            'fuwu' => $fuwu,
            'fuwu_sc' => $fuwu_sc,
            'user_id' => $user_id,
            'status' => 1,
            'x_status' => 2,
            'number' => date("YmdHis") . mt_rand(100000, 999999),
            'title' => $farr['name'],
            'package_vip_price' => 0,
            'is_package' => $is_package,
        ];
        $bool = Db::name("order")->insert($data);
        if ($bool) {
            $this->success('下单成功');
        }
        $this->error('下单失败');
    }
}
